﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using dbPoll_Application.Models;
using System.Web.Mvc;

namespace dbPoll_Application.ViewModels
{
    public class EditParticipantsViewModel
    {
        public int PollID { get; set; }
        public String PollName { get; set; }
        public Boolean isGroup { get; set; }
        public IList<ParticipantField> Fields { get; set; }
        public IList<bool> hasdem;
        public IList<bool> hascom;
        public IList<Entity> Entities { get; set; }
        public IList<EditParticipantViewModel> Participants { get; set; }

        public EditParticipantsViewModel()
        {
            this.PollID = -1;
            this.PollName = String.Empty;
            this.isGroup = false;
            this.Fields = null;
            this.Entities = null;
            this.Participants = null;
        }

        public EditParticipantsViewModel(Poll poll)
        {
            this.PollID = poll.pollID;
            this.PollName = poll.name;
            this.isGroup = poll.isGroup;
            this.Fields = poll.participantFields;
            hasdem = new List<bool>();
            hascom = new List<bool>();
            foreach (ParticipantField f in Fields)
            {
                hasdem.Add(poll.questions.Where
                    (q => q.type == QuestionType.Demographic)
                    .Cast<DemographicQuestion>().Any(p => p.linkedField != null
                        && p.linkedField.fieldID == f.fieldID));
                hascom.Add(poll.questions.Where(q => q.type == QuestionType.Demographic)
                    .Cast<DemographicQuestion>().Any(p => p.linkedField != null
                        && p.linkedField.fieldID == f.fieldID && p.compareWith != null));
            }
            this.Entities = poll.entities;
            this.Participants = new List<EditParticipantViewModel>();
            foreach (var participant in poll.participants)
            {
                var fields = new List<ParticipantFieldValueViewModel>();
                foreach (var field in poll.participantFields)
                {
                    var fieldValue = new ParticipantFieldValueViewModel();
                    fieldValue.FieldID = field.fieldID;
                    fieldValue.Name = field.name;
                    var value = field.fieldValues.Where(m => m.participant == participant).FirstOrDefault<ParticipantFieldValue>();
                    if (value == null)
                    {
                        fieldValue.Value = String.Empty;
                    }
                    else
                    {
                        fieldValue.Value = value.value;
                    }
                    fields.Add(fieldValue);
                }
                this.Participants.Add(new EditParticipantViewModel(participant.participantID, participant.deviceID, participant.group, participant.firstName, participant.lastName, fields, participant.entity, participant.votingWeight));
            }
            this.Participants=this.Participants.OrderBy(p => p.DeviceID).OrderBy(p => p.FirstName).OrderBy(p => p.LastName).ToList();
        }
    }
}
